﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
using System.Text;
using System.Web.UI.WebControls;
using System.Collections;
using System.Security.Cryptography;

namespace PromotionApp.Utility
{
    public class PageHelper
    {
        #region Private Method

        private static string GetString(byte[] data)
        {
            StringBuilder Results = new StringBuilder();

            foreach (byte b in data)
            {
                Results.Append(b.ToString("X2"));
            }

            return Results.ToString();
        }

        private static byte[] GetBytes(string data)
        {
            byte[] Results = new byte[data.Length / 2];

            for (int i = 0; i < data.Length; i += 2)
            {
                Results[i / 2] = Convert.ToByte(data.Substring(i, 2), 16);
            }

            return Results;
        }

        #endregion

        #region Public Method

        public static void SetDefautlDropDownList(DropDownList ddl, string strValue)
        {
            int index = 0;

            foreach (ListItem o in ddl.Items)
            {
                if (o.Value.Equals(strValue, StringComparison.OrdinalIgnoreCase))
                {
                    ddl.SelectedIndex = index;
                    break;
                }

                index += 1;
            }
        }

        public static void BindDropDownList<T>(DropDownList ddl, T list, string strTextField, string strValueField) where T : IEnumerable
        {
            ddl.DataSource = list;
            ddl.DataTextField = strTextField;
            ddl.DataValueField = strValueField;
            ddl.DataBind();
        }

        public static void AlertMessage(string strMessage, System.Web.UI.Page page)
        {
            string strScript = string.Format("alert(\"{0}\");", strMessage);
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), strScript, true);
        }

        public static void AlertMessageAndRedirect(string strMessage, string strUrl, System.Web.UI.Page page)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("alert(\"{0}\");", strMessage);
            sb.AppendFormat("window.open(\"{0}\");", strUrl);
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), sb.ToString(), true);
        }

        public static void SetGridViewSelectedRowScript(GridView gv, string strHiddenFieldId, string strEditId, System.Web.UI.Page page)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("function GridViewSelectedRow(id, value) {");

            foreach (GridViewRow r in gv.Rows)
            {
                sb.AppendFormat("document.getElementById('{0}').style.backgroundColor = 'white';", r.ClientID);                
            }

            sb.Append("document.getElementById(id).style.backgroundColor = '#c9e791';");
            sb.AppendFormat("document.getElementById('{0}').value = value;", strHiddenFieldId);
            sb.AppendFormat("document.getElementById('{0}').className = 'btnEdit';", strEditId);
            sb.AppendFormat("document.getElementById('{0}').disabled = false;", strEditId);

            sb.Append("}");
            
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), Guid.NewGuid().ToString(), sb.ToString(), true);
        }

        public static string GetImageUrl(string strPageUrl, string strImage)
        {
            string strImageUrl = string.Empty;
            int index = strPageUrl.LastIndexOf("/");
            
            strImageUrl = string.Format("{0}/{1}/{2}", strPageUrl.Substring(0, index), ConfigurationHelper.UploadImageFolder, strImage);
                        
            return strImageUrl;
        }

        //public static string EncryptedQueryString(StringDictionary dict)
        //{
        //    StringBuilder Content = new StringBuilder();

        //    foreach (string key in dict.Keys)
        //    {
        //        Content.Append(HttpUtility.UrlEncode(key));
        //        Content.Append("=");
        //        Content.Append(HttpUtility.UrlEncode(dict[key]));
        //        Content.Append("&");
        //    }

        //    Content.Remove(Content.Length - 1, 1);
        //    byte[] EncryptedData = ProtectedData.Protect(Encoding.UTF8.GetBytes(Content.ToString()), null, DataProtectionScope.LocalMachine);

        //    return GetString(EncryptedData);
        //}

        //public static StringDictionary DecryptedQueryString(string strencryptedData)
        //{
        //    StringDictionary dict = new StringDictionary();
        //    byte[] RawData = GetBytes(strencryptedData);
        //    byte[] ClearRawData = ProtectedData.Unprotect(RawData, null, DataProtectionScope.LocalMachine);
        //    string StringData = Encoding.UTF8.GetString(ClearRawData);
        //    int Index;
        //    string[] SplittedData = StringData.Split(new char[] { '&' });

        //    foreach (string SingleData in SplittedData)
        //    {
        //        Index = SingleData.IndexOf('=');
        //        dict.Add(HttpUtility.UrlDecode(SingleData.Substring(0, Index)), HttpUtility.UrlDecode(SingleData.Substring(Index + 1)));
        //    }

        //    return dict;
        //}

        //public static StringDictionary InitializeQueryStringDict(HttpRequest request)
        //{
        //    StringDictionary dict = null;

        //    if (string.IsNullOrEmpty(request.QueryString["data"]))
        //        throw new ApplicationException("Invalid query string parameter");
        //    else
        //        dict = PageHelper.DecryptedQueryString(request["data"]);

        //    return dict;
        //}        

        #endregion       
    }
}